package com.eenie.common.base

import android.content.Context
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import com.eenie.common.R
import com.qmuiteam.qmui.util.QMUIStatusBarHelper

abstract class BaseActivity : AppCompatActivity() {
    protected lateinit var mContext: Context
    protected lateinit var rootView: View
    private var mWinMode = WinMode.IMMERSE

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = this
        setWinMode(mWinMode)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        initInjector()
        rootView = layoutInflater.inflate(initContentView(), null)
        setContentView(rootView)
        initUiAndListener(rootView)
    }


    fun setStateBarColor(color: Int) {
        window.statusBarColor = color
    }

    fun setNavigationBarColor(color: Int) {
        window.navigationBarColor = color
    }


    /**
     * 设置Layout 如 R.layout.main_layout
     */
    @LayoutRes
    abstract fun initContentView(): Int


    /**
     * 注入Injector
     */
    abstract fun initInjector()

    /**
     * init UI && Listener
     * @param view
     */
    abstract fun initUiAndListener(view: View)




    fun setWinMode(winMode: WinMode) {
        mWinMode = winMode
        if (mWinMode == WinMode.FULL) {
            window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
            window.decorView
                    .setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
                            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            or View.SYSTEM_UI_FLAG_IMMERSIVE)
        } else if (mWinMode == WinMode.IMMERSE) {
            QMUIStatusBarHelper.translucent(this)
            window.decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_IMMERSIVE)
        } else {
            setStateBarColor(resources.getColor(R.color.colorPrimary))
        }
    }
}